package th.co.exat.uap.util;

public class Padding {

    private int startPage;
    private int lastPage;
    private int prevPage;
    private int nextPage;
    private int totalPage;
    private int page;
    private int numRows;
    private int rowsTotal;
    private int gotoPage;

    public Padding(int numRows, int page, int perPage) {
        if (page == 0) {
            page = 1;
        }
        this.page = page;
        this.numRows = numRows;

        this.prevPage = page - 1;
        this.nextPage = page + 1;

        this.rowsTotal = this.numRows % perPage;

        this.totalPage = (this.rowsTotal != 0) ? ((numRows / perPage) + 1) : numRows / perPage;
        this.lastPage = (this.page > 1) ? totalPage - 1 : totalPage;

        gotoPage = (this.page - 1) * perPage;
    }

    public String getHeadStatus() {
        String script = "<script type=\"text/javascript\" > " +
                        "$().ready(function() { " +
                        "   $('#firstpage').click(function(){ " +
                        "      $('#p').val('1'); " +
                        "      submit(); " +
                        "    }); " +
                        "   $('#nextpage').click(function(){ " +
                        "      $('#p').val('" + this.nextPage + "'); " +
                        "      submit(); " +
                        "    }); " +
                        "   $('#prevpage').click(function(){ " +
                        "      $('#p').val('" + this.prevPage + "'); " +
                        "      submit(); " +
                        "   }); " +
                        "   $('#totalpage').click(function(){ " +
                        "      $('#p').val('" + this.totalPage + "'); " +
                        "      submit(); " +
                        "   }); " +
                        "   $('#spage').val('" + this.page + "'); " +
                        "   $('#spage').change(function() { " +
                        "      $('#p').val($('#spage').val()); " +
                        "      submit(); " +
                        "   });" +
                        "}); " +
                        "</script>";

        String out = "\n <fieldset> \n"
                + "<div id=\"result-bar\"> \n"
                + "<table border=\"0\" width=\"100%\"> \n"
                + "  <tr> \n "
                + "  <td>ผลการค้นหา : " + this.numRows + " รายการ</td> \n "
                + "  <td width=\"200px\" align=\"right\">หน้าที่ : \n "
                + "  <select id=\"spage\"> \n";
        for (int i = 0; i < this.totalPage; i++) {
            out += "<option value=\"" + (i + 1) + "\" >" + (i + 1) + "</option>\n";

        }
        out += "</select> \n"
                + " / " + this.totalPage + " "
                + "</td>\n"
                + "<td width=\"100px\">\n";

        // First Page
        if (this.page > 1) {
            out += "<a id=\"firstpage\" href=\"#\">"
                    + "<img src=\"theme/images/ar_left_abs.gif\" border=\"0\" alt=\"หน้าแรก\" />"
                    + "</a>";
        } else {
            out += "<img src=\"theme/images/ar_left_abs_dis.gif\" /> \n";
        }

        // Prev Page
        if (this.prevPage > 0) {
            out += "<a id=\"prevpage\" href=\"#\" >"
                    + "<img src=\"theme/images/ar_left.gif\" border=\"0\" alt=\"หน้าก่อน\" />"
                    + "</a>";
        } else {
            out += "<img src=\"theme/images/ar_left_dis.gif\" />";
        }

        // Next Page
        if (this.page < this.totalPage) {
            out += "<a id=\"nextpage\" href=\"#\">"
                    + "<img src=\"theme/images/ar_right.gif\" border=\"0\" alt=\"หน้าถัดไป\" />"
                    + "</a>";
        } else {
            out += "<img src=\"theme/images/ar_right_dis.gif\" />\n";
        }

        // End Page
        if (this.page < this.totalPage) {
            out += "<a id=\"totalpage\" href=\"#\">"
                    + "<img src=\"theme/images/ar_right_abs.gif\" border=\"0\" alt=\"หน้าสุดท้าย\" />"
                    + "</a>";
        } else {
            out += "<img src=\"theme/images/ar_right_abs_dis.gif\" />\n";
        }
        out += " </td>\n"
                + " </tr>\n"
                + "  </table>\n"
                + "</div>\n"
                + "</fieldset>\n";
        return  script + out;
    }

    public int getNextPage() {
        return nextPage;


    }

    public int getTotalPage() {
        return totalPage;


    }

    public int getPage() {
        return page;


    }

    public int getPrevPage() {
        return prevPage;


    }

    public int getRowTotal() {
        return rowsTotal;
    }

    public int getGotoPage() {
        return gotoPage;
    }
}

